package gis.utils;

import android.app.Activity;
import android.widget.Toast;

import gis.network.http.UploadPhotoAndPoints;
import com.xkh.xkh.MyApplication;

import java.lang.ref.WeakReference;

import managers.UIHandler;

/**
 * 上传坐标和照片
 * Created by 凯 on 2015/11/12.
 */
public class UploadPhotoPointsThread implements Runnable {

    UploadPhotoAndPoints uploadPhotoAndPoints; // 上传照片和监测点
    WeakReference<Activity> activity;

    Runnable runnable;

    public void setRunnable(Runnable runnable) {
        this.runnable = runnable;
    }

    public UploadPhotoPointsThread(UploadPhotoAndPoints uploadPhotoAndPoints) {
        this.uploadPhotoAndPoints = uploadPhotoAndPoints;
    }

    public UploadPhotoPointsThread(UploadPhotoAndPoints uploadPhotoAndPoints, Activity activity) {
        this.uploadPhotoAndPoints = uploadPhotoAndPoints;
        this.activity = new WeakReference<Activity>(activity);
    }

    @Override
    public void run() {
        try {
            if (uploadPhotoAndPoints.upload())
            {
                UIHandler.uiHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyApplication.share(),"上传成功",Toast.LENGTH_SHORT).show();
                        if (runnable != null)
                            runnable.run();
                        if (activity.get() != null)
                        {
                            activity.get().finish();
                        }
                    }
                });
            }
            else
            {
                UIHandler.uiHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyApplication.share(),"上传失败",Toast.LENGTH_SHORT).show();
                        if (runnable != null)
                            runnable.run();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
